<?php

if (!defined('YAR_WEB_PAGE_TO_ROOT')) {
    define('YAR_WEB_PAGE_TO_ROOT', '../');
}

if (!@($GLOBALS["___mysqli_ston"] = mysqli_connect($_YAR['db_server'], $_YAR['db_user'], $_YAR['db_password']))) {
    yarMessagePush("不能连接到数据库，请检查配置文件或数据库服务状态。");
    if ($_YAR['db_user'] == "root") {
        yarMessagePush('数据库用户为root, 请注意。');
    }
    yarPageReload();
}


// Create database
$drop_db = "DROP DATABASE IF EXISTS {$_YAR[ 'db_database' ]};";
if (!@mysqli_query($GLOBALS["___mysqli_ston"], $drop_db)) {
    yarMessagePush("不能移除存在的数据库。<br />SQL: " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
    yarPageReload();
}

$create_db = "CREATE DATABASE {$_YAR[ 'db_database' ]};";
if (!@mysqli_query($GLOBALS["___mysqli_ston"], $create_db)) {
    yarMessagePush("不能创建数据库。<br />SQL: " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
    yarPageReload();
}
yarMessagePush("数据库创建成功。");


// Create table 'users'
if (!@((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . $_YAR['db_database']))) {
    yarMessagePush('不能连接到数据库。');
    yarPageReload();
}

$create_tb = "CREATE TABLE users (user_id int(6),first_name varchar(15),last_name varchar(15), user varchar(15), password varchar(32),avatar varchar(70), last_login TIMESTAMP, failed_login INT(3), PRIMARY KEY (user_id));";
if (!mysqli_query($GLOBALS["___mysqli_ston"], $create_tb)) {
    yarMessagePush("不能创建表。<br />SQL: " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
    yarPageReload();
}
yarMessagePush("'users' 表已创建成功。");


// Insert some data into users
$base_dir = str_replace("setup.php", "", $_SERVER['SCRIPT_NAME']);
$avatarUrl = $base_dir . 'hackable/users/';

$insert = "INSERT INTO users VALUES
	('1','admin','admin','admin',MD5('password'),'{$avatarUrl}admin.jpg', NOW(), '0'),
	('2','Gordon','Brown','gordonb',MD5('abc123'),'{$avatarUrl}gordonb.jpg', NOW(), '0'),
	('3','Hack','Me','1337',MD5('charley'),'{$avatarUrl}1337.jpg', NOW(), '0'),
	('4','Pablo','Picasso','pablo',MD5('letmein'),'{$avatarUrl}pablo.jpg', NOW(), '0'),
	('5','Bob','Smith','smithy',MD5('password'),'{$avatarUrl}smithy.jpg', NOW(), '0');";
if (!mysqli_query($GLOBALS["___mysqli_ston"], $insert)) {
    yarMessagePush("数据不能插入 'users' 表<br />SQL: " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
    yarPageReload();
}
yarMessagePush("数据已插入 'users' 表。");


// Create guestbook table
$create_tb_guestbook = "CREATE TABLE guestbook (comment_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, comment varchar(300), name varchar(100), PRIMARY KEY (comment_id));";
if (!mysqli_query($GLOBALS["___mysqli_ston"], $create_tb_guestbook)) {
    yarMessagePush("不能创建表。<br />SQL: " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
    yarPageReload();
}
yarMessagePush("'guestbook' 表已创建成功。");


// Insert data into 'guestbook'
$insert = "INSERT INTO guestbook VALUES ('1','This is a test comment.','test');";
if (!mysqli_query($GLOBALS["___mysqli_ston"], $insert)) {
    yarMessagePush("数据不能插入 'guestbook' 表<br />SQL: " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
    yarPageReload();
}
yarMessagePush("数据已插入 'guestbook' 表.");


// Copy .bak for a fun directory listing vuln
$conf = YAR_WEB_PAGE_TO_ROOT . 'config/config.inc.php';
$bakconf = YAR_WEB_PAGE_TO_ROOT . 'config/config.inc.php.bak';
if (file_exists($conf)) {
    // Who cares if it fails. Suppress.
    @copy($conf, $bakconf);
}

yarMessagePush("备份文件 /config/config.inc.php.bak 已创建。");

// Done
yarMessagePush("安装完成！");

if (!yarIsLoggedIn())
    yarMessagePush("Please <a href='login.php'>login</a>.<script>setTimeout(function(){window.location.href='login.php'},5000);</script>");
yarPageReload();

?>
